package dao;

import model.Goods;
import util.JDBCUtil;
import model.User;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
    *
    * User表的处理(增删改)--逻辑处理
    */
public class UserDao {
    private JDBCUtil jdbcUtil;
    /**
     *查询用户信息
     */
    public UserDao() {
        jdbcUtil = new JDBCUtil();
    }

//  根据用户名、密码查询
public User getUserById(int userId) {
    JDBCUtil jdbcUtil = new JDBCUtil();
    String sql = "SELECT * FROM user WHERE user_id = ?";
    ResultSet resultSet = null;
    User user = null;

    try {
        resultSet = jdbcUtil.prepareQuery(sql, userId);
        if (resultSet.next()) {
            user = new User();
            user.setId(resultSet.getInt("user_id"));
            user.setUsername(resultSet.getString("username"));
            user.setUser_type(resultSet.getInt("user_type"));
            user.setPhone(resultSet.getString("phone"));
            user.setCreated_at(resultSet.getString("created_at"));
            user.setPassword(resultSet.getString("password"));
            user.setEmail(resultSet.getString("email"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (resultSet != null) resultSet.close();
            jdbcUtil.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return user;
}
    public Goods getGoodsById(int userId) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "SELECT * FROM goods WHERE user_id = ?";
        ResultSet resultSet = null;

        Goods goods=null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, userId);
            if (resultSet.next()) {
                goods=new Goods();
//                goods.setUser_id(resultSet.getInt("user_id"));
//                goods.setGoods_id(resultSet.getInt("goods_id"));
                goods.setGoods_info(resultSet.getString("goods_info"));
                goods.setGoods_category(resultSet.getString("goods_category"));
                goods.setGoods_price(resultSet.getInt("goods_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return goods;
    }
    public Goods getGoodsByGOODSId(int goodsId) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "SELECT goods_price FROM goods WHERE goods_id = ?";
        ResultSet resultSet = null;

        Goods goods = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, goodsId);
            if (resultSet.next()) {
                goods = new Goods();
                goods.setGoods_id(resultSet.getInt("goods_id"));
                goods.setGoods_info(resultSet.getString("goods_info"));
                goods.setGoods_category(resultSet.getString("goods_category"));

                goods.setGoods_price(resultSet.getDouble("goods_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return goods;
    }
    public int getUserIdByUsername(String username) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        int user_Id =0;
        String sql = "SELECT user_id FROM user WHERE username = ?";
        ResultSet resultSet = null;

        try {
            resultSet = jdbcUtil.prepareQuery(sql, username);
            if (resultSet.next()) {
                 user_Id  =resultSet.getInt("user_id");
            }
    } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user_Id;
    }


    public boolean updateUser1(User user) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "UPDATE user SET username = ?, user_type = ?, phone = ?, password = ?, email = ? WHERE user_id = ?";
        boolean isUpdated = false;

        try {
            boolean rowsAffected = jdbcUtil.paramsUpdate(sql, user.getUsername(), user.getUser_type(), user.getPhone(), user.getPassword(), user.getEmail(), user.getId());
            isUpdated = rowsAffected;
        } finally {
            jdbcUtil.closeConnection();
        }
        return isUpdated;
    }
    public boolean updateUser(User user) {
        String sql = "UPDATE user SET username = ?, user_type = ?, phone = ?, email = ? WHERE user_id = ?";
        return jdbcUtil.paramsUpdate(sql, user.getUsername(), user.getUser_type(), user.getPhone(), user.getEmail(), user.getId());
    }
public User findUser(String username, String password) {
    User user = null;
    JDBCUtil jdbcUtil = null;
    ResultSet resultSet = null;

    try {
        jdbcUtil = new JDBCUtil();
        String sql = "SELECT * FROM user WHERE username=? AND password=?";
        resultSet = jdbcUtil.prepareQuery(sql, username, password);

        if (resultSet.next()) {
            user = new User();
            user.setId(resultSet.getInt("user_id"));
            user.setUsername(resultSet.getString("username"));
            user.setEmail(resultSet.getString("email"));
            user.setPhone(resultSet.getString("phone"));
            user.setUser_type(resultSet.getInt("user_type"));
            user.setCreated_at(resultSet.getString("created_at"));
        }
    } catch (SQLException e) {
        e.printStackTrace(); // You can also use a logging framework
    } finally {
        try {
            if (resultSet != null) resultSet.close();
            if (jdbcUtil != null) jdbcUtil.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return user;
}

//  根据模糊用户名进行查询
public List<User> search(String searchUsername) {
    ResultSet resultSet = null;
    List<User> userList = new ArrayList<>();

    try {
        String sql = "SELECT * FROM user WHERE username LIKE ?";
        resultSet = jdbcUtil.prepareQuery(sql, "%" + searchUsername + "%");

        while (resultSet.next()) {
            User user = new User();
            user.setId(resultSet.getInt("user_id"));
            user.setUsername(resultSet.getString("username"));
            user.setEmail(resultSet.getString("email"));
            user.setPhone(resultSet.getString("phone"));
            user.setUser_type(resultSet.getInt("user_type"));
            user.setCreated_at(resultSet.getString("created_at"));
            userList.add(user);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        jdbcUtil.closeConnection();
    }

    return userList;
}
//用户根据id查询
//    public User getUserById(int userId) {
//        ResultSet resultSet = null;
//        User user = null;
//
//        String sql = "SELECT * FROM user WHERE user_id = ?";
//
//        try {
//            resultSet = jdbcUtil.prepareQuery(sql, String.valueOf(userId));
//            if (resultSet.next()) {
//                user = new User();
//                user.setId(resultSet.getInt("user_id"));
//                user.setUsername(resultSet.getString("username"));
//                user.setEmail(resultSet.getString("email"));
//                user.setPhone(resultSet.getString("phone"));
//                user.setUser_type(resultSet.getInt("user_type"));
//                user.setCreated_at(resultSet.getString("created_at"));
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if (resultSet != null) {
//                try {
//                    resultSet.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//            jdbcUtil.closeConnection();
//        }
//
//        return user;
//    }
//查询用户列表所有信息
    public List<User> allquiry() {
        List<User> userList = new ArrayList<>();
        ResultSet resultSet = null;

        try {
            String sql = "SELECT * FROM user"; // 假设数据库表名为 users
            resultSet = jdbcUtil.prepareQuery(sql);
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("user_id"));
                user.setUsername(resultSet.getString("username"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setUser_type(resultSet.getInt("user_type"));
                user.setCreated_at(resultSet.getString("created_at"));
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            jdbcUtil.closeConnection();
        }

        return userList;
    }
//    根据id查找，并删除
public boolean delete(int userId){
    JDBCUtil jdbcUtil = new JDBCUtil();

    try {
        String sql = "DELETE FROM `user` WHERE user_id = ?";
        boolean pstmt = jdbcUtil.paramsUpdate(sql,userId);

        return  pstmt; // 如果影响的行数大于0，表示删除成功
    }
    // 处理数据库异常
    finally {
        jdbcUtil.closeConnection(); // 关闭数据库连接
    }
}


    public boolean checkPassword(String username, String oldPassword) {
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        try (ResultSet resultSet = jdbcUtil.prepareQuery(sql, username, oldPassword)) {
            return resultSet.next(); // 如果查询结果存在记录，则返回 true，否则返回 false
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 发生异常时返回 false
        }
    }

    public boolean updatePassword(String username, String newPassword) {
        String sql = "UPDATE user SET password = ? WHERE username = ?";
        return jdbcUtil.paramsUpdate(sql, newPassword, username); // 返回更新结果
    }







}
